/*
 * Copyright (c) 2018-2023 北京华恒盛世科技有限公司
 * QUICKPOOL is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *     http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
*/

#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <rpc/types.h>
#include <netdb.h>
#include <errno.h>
#include <malloc.h>
#include <limits.h>

#include "common.h"

char *
gf_getwd(char *path)
{
    char *pwd;
    struct stat pwdstat, dotstat;
    char tempBuff[PATH_MAX+1];

    pwd = getenv("PWD");
    if (pwd == NULL && (pwd = getenv("CWD")) == NULL) {
        if (getcwd(tempBuff, PATH_MAX)) {

           strncpy(path, tempBuff, PATH_MAX-1);
	   return(path);
	} else {
	   return NULL;
        }
    }

    if (stat(pwd, &pwdstat) == 0 && stat(".", &dotstat) == 0) {
	if (pwdstat.st_dev == dotstat.st_dev && pwdstat.st_ino == dotstat.st_ino) {

            strncpy(path, pwd, PATH_MAX-1);
	    return path;
	}
    }

    if(getcwd(tempBuff, PATH_MAX)) {
        strncpy(path, tempBuff, PATH_MAX-1);
	return(path);
    }

    return NULL;
} 

int
gf_isfullpath(const char *path)
{
    if (path[0] ==  '/')
        return TRUE;
    else
        return FALSE;
}
